{include file="index/_meta" /}
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-input-inline">
                <input type="text" placeholder="输入关键字" class="layui-input" name="username"/>
            </div>
            <div class="layui-input-inline">
                <select name="role_id">
                    <option value="0">全部</option>
                    {:widget('common/common/getUserRoleSelect')}
                </select>
            </div>
            <button class="layui-btn" lay-submit lay-filter="search">&nbsp;&nbsp;搜索&nbsp;&nbsp;</button>
        </div>
        <div class="layui-card-body">
            <div class="layui-btn-group" style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-role" data-type="add">添加</button>
                <button class="layui-btn layuiadmin-btn-role" data-type="batchdel">删除</button>
            </div>

            <table id="KT_table" lay-filter="KT_table"></table>

            <script type="text/html" id="KT_table_temp">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
        </div>
    </div>
</div>

<script src="__STATIC__/layuiadmin/layui/layui.js"></script>
<script>
    layui.use(['table','form'], function () {
        var $ = layui.jquery
            ,table = layui.table
            ,form = layui.form;

        var ktTable = table.render({
            elem: '#KT_table'
            ,url: "{:url('user/user_index')}"
            ,page: true //开启分页
            ,cols: [[ //表头
                {field: 'id', title: 'ID',ort: true}
                ,{field: 'username', title: '用户名'}
                ,{field: 'password', title: '密码'}
                ,{field: 'role', title: '角色'}
                ,{ title: '操作',templet:"#KT_table_temp"}
            ]]
        });


        var active = {
            add:function () {
                add('添加',"user_add.html");
            },
            batchdel:function () {
                var checkStatus = table.checkStatus('KT_table'),
                    data = checkStatus.data,
                    newsId = [];
                if(data.length > 0) {
                    for (var i in data) {
                        newsId.push(data[i].id);
                    }
                    layer.confirm('确定删除？', {icon: 3, title: '提示信息'}, function (index) {
                        $.post("{:url('user/userDel')}",{
                            id : newsId
                        },function(data){
                            ktTable.reload();
                            layer.close(index);
                        })
                    })
                }else{
                    layer.msg("请选择需要删除的数据");
                }
            }
        }
        $('.layui-btn.layuiadmin-btn-role').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


        table.on('tool(KT_table)', function(obj){
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr;

            if(layEvent === 'del'){ //删除
                layer.confirm('真的删除行么', function(index){

                    $.post("{:url('user/userDel')}",{
                        id : data.id
                    },function(data){
                        ktTable.reload();
                        layer.close(index);
                    })
                    obj.del();
                    layer.close(index);

                });
            } else if(layEvent === 'edit'){ //编辑
                add("编辑","user_edit.html?id="+data.id)
            }
        });

        //监听搜索
        form.on('submit(search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('KT_table', {
                where: field
            });
        });
    });
</script>
</body>
</html>

